/*
 * Copyright 2011 evswork Team
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *     http://www.apache.org/licenses/LICENSE-2.0
 *  
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * 
 */

package org.evswork.getwit;

import org.evswork.getwit.smackwrap.Talk;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PagerCtlWrapper implements OnClickListener, android.content.DialogInterface.OnClickListener {
	GttMainActivity act;
	ImageButton CloseImBtn, PreImBtn, NxtImBtn;
	Button CurBtn;
	TextView tv;
	LinearLayout root;
	int page;
	String prefix; //"-ho p123" -> prefix = "ho"
	
	PagerCtlWrapper (GttMainActivity act) {
		this.act = act;
		bindView();
	}
	
	private void requestPage() {
		Talk.send(String.format("-%s p%d", prefix,page));
	}
	
	
	private void bindView() {
		root = (LinearLayout)act.findViewById(R.id.pager_root);
		
		PreImBtn = (ImageButton) root.findViewById(R.id.pager_pre_btn);
		CurBtn = (Button) root.findViewById(R.id.pager_cur_btn);
		NxtImBtn = (ImageButton) root.findViewById(R.id.pager_nxt_btn);
		CloseImBtn = (ImageButton) root.findViewById(R.id.pager_close_btn);
		
		PreImBtn.setOnClickListener(this);
		CurBtn.setOnClickListener(this);
		NxtImBtn.setOnClickListener(this);
		CloseImBtn.setOnClickListener(this);
		
		tv = (TextView) root.findViewById(R.id.pager_text);
	}
	
	View layout;
	EditText edit;
	public void showUserNameDialog() {
		layout = View.inflate(act, R.layout.edit, null);
		edit = (EditText)layout.findViewById(R.id.edit_root);
		new AlertDialog.Builder(act)
			.setTitle("输入用户名:")
			.setView(layout)
			.setPositiveButton("确定",
				new DialogInterface.OnClickListener() {

					@Override
					public void onClick(
							DialogInterface dialog,
							int which) {
						show("tl "+edit.getText().toString(),true);
					}
				
				}
			)
			.setNegativeButton("取消", null)
			.show();
	}
	
	public void showPageDialog() {
		new AlertDialog.Builder(act)
			.setTitle("请求何种页面?")
			//.setSingleChoiceItems(act.getResources().getStringArray(R.array.req_page), 0, this)
			.setItems(act.getResources().getStringArray(R.array.req_page), this)
			.show();
	}
	
	public void show(String p,boolean req) {
		prefix = p;
		page = 1;
		if (req) {
			requestPage();
		}
		tv.setText(prefix);
		CurBtn.setText("1");
		root.setVisibility(View.VISIBLE);
	}
	
	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.pager_pre_btn:
			if (page > 1) {
				--page;
				CurBtn.setText(String.valueOf(page));
				requestPage();
			}
			break;
		case R.id.pager_nxt_btn:
			++ page;
			CurBtn.setText(String.valueOf(page));
			requestPage();
			break;
		case R.id.pager_cur_btn:
			requestPage();
			break;
		case R.id.pager_close_btn:
			root.setVisibility(View.GONE);
			break;
		}
	}

	@Override
	public void onClick(DialogInterface dialog, int which) {
		switch (which) {
		case 0: //fav
			show("fav",true);
			break;
		case 1: //d
			show("d",true);
			break;
		case 2: //@
			show("@",true);
			break;
		case 3: //rt
			show("rt",true);
			break;
		case 4: //me
			show("me",true);
			break;
		case 5: //ho
			show("ho",true);
			break;
		case 6: //tl
			showUserNameDialog();
			break;
		}
		dialog.dismiss();
		
	}
}
